
/*********************
 *      INCLUDES
 *********************/
#include "driver_gpio.h"
#include "fr30xx.h"
#include "app_lvgl.h"

#include "fr_device_pmu_io.h"
#include "fr_device_button.h"
#include "fr_device_encode.h"
/*********************
 *      DEFINES
 *********************/



#define BUTTON_DEBUG(fmt,arg...)   printf("[BUTTON]"fmt,##arg)


/**********************
 *      TYPEDEFS
 **********************/




/**********************
 *  STATIC PROTOTYPES
 **********************/


/**********************
 *  STATIC VARIABLES
 **********************/



/**********************
 *  VARIABLES
 **********************/
 
/**********************
 *      MACROS
 **********************/



/**********************
 *   EXTERN FUNCTIONS
 **********************/



/**********************
 *   FUNCTIONS
 **********************/

void device_pmu_io_init(void)
{
#if BOARD_SEL == BOARD_EVB_FR5090
    pmu_gpio_int_init(BUTTON_PIN_NUM, PMU_GPIO_PULL_UP, 1);
    pmu_gpio_int_init(SOS_KEY_PIN_NUM, PMU_GPIO_PULL_UP, 1);
    pmu_gpio_int_init(KEY1_PIN_NUM, PMU_GPIO_PULL_UP, 1);
    pmu_gpio_int_init(KEY2_PIN_NUM, PMU_GPIO_PULL_UP, 1);
    pmu_gpio_int_init(ENCONDED_A_PIN_NUM, PMU_GPIO_PULL_UP, 1);
    pmu_gpio_int_init(ENCONDED_B_PIN_NUM, PMU_GPIO_PULL_UP, 1);
#elif BOARD_SEL == BOARD_EVB_FR3092E_RGB

    #if BOARD_VERSION == BOARD_V1_0
        pmu_gpio_int_init(BUTTON_PIN_NUM, PMU_GPIO_PULL_UP, 1);
        pmu_gpio_int_init(SOS_KEY_PIN_NUM, PMU_GPIO_PULL_UP, 1);
        pmu_gpio_int_init(KEY1_PIN_NUM, PMU_GPIO_PULL_UP, 1);
        pmu_gpio_int_init(KEY2_PIN_NUM, PMU_GPIO_PULL_UP, 1);
        pmu_gpio_int_init(ENCONDED_A_PIN_NUM, PMU_GPIO_PULL_UP, 1);
        pmu_gpio_int_init(ENCONDED_B_PIN_NUM, PMU_GPIO_PULL_UP, 1);
    #elif BOARD_VERSION == BOARD_V1_3
        pmu_gpio_int_init(BUTTON_PIN_NUM, PMU_GPIO_PULL_UP, 1);
        pmu_gpio_int_init(KEY1_PIN_NUM, PMU_GPIO_PULL_UP, 1);
        pmu_gpio_int_init(KEY2_PIN_NUM, PMU_GPIO_PULL_UP, 1);
    #else
    #error "choose correct board"
    #endif

#else
#error "choose correct board"
#endif

    pmu_enable_isr(PMU_GPIO_PMU_INT_MSK_BIT);
    NVIC_SetPriority(PMU_IRQn, 4);
    NVIC_EnableIRQ(PMU_IRQn);
}

void PMU_GPIO_PMU_IRQHandler(void)
{
    uint16_t data = ool_read16(PMU_REG_PIN_DATA);
    uint16_t result = ool_read16(PMU_REG_PIN_XOR_RESULT);
    /* update last value with latest data */
    ool_write16(PMU_REG_PIN_LAST_V, data);
    /* clear last XOR result */
    ool_write16(PMU_REG_PIN_XOR_CLR, result);

//    printf("PMU IO: 0x%04x, 0x%04x\r\n", data, result);

    if (data & PMU_PIN_9) {
        system_prevent_sleep_clear(SYSTEM_PREVENT_SLEEP_TYPE_HCI_RX);
    }
    else {
        system_prevent_sleep_set(SYSTEM_PREVENT_SLEEP_TYPE_HCI_RX);      
    }

#if BOARD_SEL == BOARD_EVB_FR5090
    if (result & (ENCONDED_A_PIN_NUM | ENCONDED_B_PIN_NUM)) {
        uint8_t encode_bit_status;

        if (!(data & ENCONDED_A_PIN_NUM))
        {
            encode_bit_status |= 0x02;
        }
        else
        {
            encode_bit_status &= 0xFD;
        }

        if (!(data & ENCONDED_B_PIN_NUM))
        {
            encode_bit_status |= 0x01;
        }
        else
        {
            encode_bit_status &= 0xFE;
        }

        encode_toggle_detected(encode_bit_status);
    }
    
    if (result & (BUTTON_PIN_NUM | SOS_KEY_PIN_NUM | KEY1_PIN_NUM | KEY2_PIN_NUM)) {
        button_toggle_detected(data);
    }
#elif BOARD_SEL == BOARD_EVB_FR3092E_RGB

    #if BOARD_VERSION == BOARD_V1_0
        if (result & (ENCONDED_A_PIN_NUM | ENCONDED_B_PIN_NUM)) {
            uint8_t encode_bit_status;

            if (!(data & ENCONDED_A_PIN_NUM))
            {
                encode_bit_status |= 0x02;
            }
            else
            {
                encode_bit_status &= 0xFD;
            }

            if (!(data & ENCONDED_B_PIN_NUM))
            {
                encode_bit_status |= 0x01;
            }
            else
            {
                encode_bit_status &= 0xFE;
            }

            encode_toggle_detected(encode_bit_status);
        }
        
        if (result & (BUTTON_PIN_NUM | SOS_KEY_PIN_NUM | KEY1_PIN_NUM | KEY2_PIN_NUM)) {
            button_toggle_detected(data);
        }
    #elif BOARD_VERSION == BOARD_V1_3
        if (result & (BUTTON_PIN_NUM | KEY1_PIN_NUM | KEY2_PIN_NUM)) {
            button_toggle_detected(data);
        }
    #else
    #error "choose correct board"
    #endif

#else
#error "choose correct board"
#endif


}
 
